<?php
/*
+ ---------------------------------------------------------------------- +
| e107Shop - An e-commerce plugin for the e107 content management system |
|                                                                        |
| ByOrder Europe 2007-2009                                               |
| http://e107shop.com                                                    |
| pbielen@gmail.com                                                      |
|                                                                        |
| Released under the terms and conditions of the LGPL                    |
+ ---------------------------------------------------------------------- +
*/

//
// Include e107 class, Header and the e107Shop classes
//
require_once("../../class2.php");
require_once("library/api.php");
require_once(e_HANDLER."np_class.php");
require_once("shortcodes/navigator_shortcodes.php");
require_once("shortcodes/product_shortcodes.php");
if (file_exists(THEME."navigator.php")) require_once(THEME."navigator.php");
else require_once("templates/navigator.php");
if (file_exists(THEME."product_details_template.php")) require_once(THEME."product_details_template.php");
else require_once("templates/product_details_template.php");
require(HEADERF);

e107shop_display_custom_page_info(e_PAGE, "top");

// Get the product ID
$product_id=$_GET["product"];
$category_id = intval($_GET["cat"]);

// Count the categories, if no categories to jump to there is no need for the navbar.
// we use select instead of count here, cause we want to fetch if more than one cat avoiding an extra db call
$countcats = $sql -> db_Select($e107shop_db_name['categories'], "*", "parid='' and active=1");
$text = '';
if ($countcats > 1) {
    // Start the row for the navigation
    $sql->db_Select($e107shop_db_name['categories'], "*", "catId='".$category_id."'");
    $row = $sql->db_Fetch();
    $parentid = $row['parid'];
    $navigation[0] = $row['catName'];
    $counter = 1;
    while (!$parentid == '') {
        $sql->db_select($e107shop_db_name['categories'], "*", "catId='".$parentid."'");
        while ($row = $sql->db_Fetch(MYSQLI_ASSOC)) {
            $parentid = $row['parid'];
            $navigation[$counter] = $row['catId'];
            $counter++;
            $navigation[$counter] = $row['catName'];
            $counter++;
        }
    }
    cachevars('e107shopnavigationlist', $navigation);
    $text .= $tp->parseTemplate($NAVIGATOR_PRODUCT_TEMPLATE, FALSE, $navigator_shortcodes);
}

if (!$sql -> db_Select($e107shop_db_name['categories'], "*", "catId='".$category_id."' AND (category_class IN (".USERCLASS_LIST."))")){    
    // No access to this category
    $text .= "<div style='text-align:center;'>".lan_global_no_access."</div>";
    return $details;
    exit();
} else {
    while ($row = $sql->db_Fetch(MYSQLI_ASSOC)) {
        $category_order_class= $row['category_order_class'];
    }
}

// get data from table and prepare for shortcodes
$sql->db_Select($e107shop_db_name['products'], "*", "ID='".$product_id."'");
$row = $sql->db_Fetch(MYSQLI_ASSOC);
// check if the product has options
$optionscount = $sql->db_Count($e107shop_db_name['product_options'], "(*)", "WHERE itemid='".$product_id."'");
if ($optionscount) $row['product_options'] = e107shop_get_products_options($product_id);
cachevars('e107shopproductdetails', $row);

$text .="<form name='products' action='cart.php?action=add' method='post'>";
$text .= $tp->parseTemplate($PRODUCTS_DETAILS_TEMPLATE, FALSE, $product_shortcodes);
$text .="</form>";

if ($countcats > 1) {
    $text .= $tp->parseTemplate($NAVIGATOR_PRODUCT_TEMPLATE, FALSE, $navigator_shortcodes);
}
$ns->tablerender($shop_settings["store_name"], $text);

e107shop_display_custom_page_info(e_PAGE, "bottom");

require(FOOTERF);
